import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

public class MobileAgent implements Runnable, Persistent{
	
    private static final int NULL = 0;
    private static final int INTEGER = 1;
    private static final int STRING = 2;
    
    private Vector contacts;	//string
    private Vector times;		//integer
	
	/*constructor*/
	public MobileAgent(){}
	
	/**
	 * run method still to be completed
	 */
	public void run(){
		
	}
	
	public String getNextDest(){
		return this.contacts.elementAt(0).toString();
	}
	
    public byte[] persist() throws IOException {
    	
    	ByteArrayOutputStream bout =  new ByteArrayOutputStream();
    	DataOutputStream      dout =  new DataOutputStream( bout );

    	int n = this.contacts.size();
    	int m = this.times.size();
    	dout.writeInt( n+m );
    	/**
    	 * first write out contacts 
    	 * */
    	for( int i = 0; i < n; ++i ){    		
            Object o = this.contacts.elementAt( i );
              if( o instanceof String ){
                  dout.writeByte( STRING );
                  dout.writeUTF( (String) o );
              } 
              else if( o instanceof Integer ){
                  dout.writeByte( INTEGER );
                  dout.writeInt( 
                              ((Integer) o).intValue() );
              } 
              else if( o == null ){
                  dout.writeByte( NULL );
              } 
              else {
                  throw new IOException( 
                  "Cannot persist " + "object of type " + o.getClass().getName() );
              }
          }
    	/**
    	 * then write out times 
    	 * */
    	for( int i = 0; i < m; ++i ){    		
            Object o = this.times.elementAt( i );
              if( o instanceof String ){
                  dout.writeByte( STRING );
                  dout.writeUTF( (String) o );
              } 
              else if( o instanceof Integer ){
                  dout.writeByte( INTEGER );
                  dout.writeInt( 
                              ((Integer) o).intValue() );
              } 
              else if( o == null ){
                  dout.writeByte( NULL );
              } 
              else {
                  throw new IOException( 
                  "Cannot persist " + "object of type " + o.getClass().getName() );
              }
          }
    	
        dout.flush();

        return bout.toByteArray();    	
    }
    
    public void resurrect( byte[] persisted ) throws IOException {
    	ByteArrayInputStream bin = new ByteArrayInputStream( persisted );
    	DataInputStream      din = new DataInputStream( bin );

    	int n = din.readInt();
    	
    	for( int i = 0; i < n; ++i ){
            int type = din.readByte();
            if( type == NULL ){
               
            } 
            else if( type == INTEGER ){
            	this.times.addElement( new Integer( din.readInt() ) );
            } 
            else if( type == STRING ){
                this.contacts.addElement( din.readUTF() );
            } 
            else {
				throw new IOException( "Unknown " +
				"type " + type );
            }
        }  
    }
}